﻿using Sidvall.Serialization;
using System.Linq;

namespace Sidvall.WorkItemManager.Data.ConfigModel.EntityManagers
{
    public sealed partial class IdentityProviderManager<TEntity, TList> : global::Sidvall.Data.EntityManagerBase
        where TEntity : global::Sidvall.WorkItemManager.Data.IIdentityProvider
        where TList : Sidvall.Data.IDataRowProvider<IIdentityProvider>
    {
        #region Public Members

        #region AcceptChanges

        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IIdentityProvider value)
        {
            AcceptChanges(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IIdentityProvider value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (value == null)
                return;
            if (value.DataRowStateIsDirty)
            {
                if (acceptChangesSettings == null)
                    acceptChangesSettings = new Sidvall.Data.AcceptChangesSettings();
                Sidvall.Data.EntityManager.AcceptItemChanges(value, acceptChangesSettings);
            }
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> value)
        {
            AcceptItemsChanges(value, null);
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptItemsChanges(value, acceptChangesSettings);
        }

        #endregion
        #region SetBusinessSettings

        public void SetBusinessSettings(global::Sidvall.WorkItemManager.Data.IIdentityProvider value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessSettings);
        }
        public void SetBusinessSettings(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessCollectionSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessCollectionSettings);
            foreach (var item in value)
                SetBusinessSettings(item, businessSettings, businessCollectionSettings);
        }

        #endregion
        #region GetDataRowStateCounts

        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(global::Sidvall.WorkItemManager.Data.IIdentityProvider value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, global::Sidvall.WorkItemManager.Data.IIdentityProvider value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.IdentityProvider, value);
        }
        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.IdentityProvider, value as Sidvall.Data.IListContextProvider);
            foreach (var item in value)
                LoadDataRowStateCounts(items, item);
        }

        #endregion
        #region LoadInstanceField

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void LoadInstanceField(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IIdentityProvider> items, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (items == null)
                return;
            foreach (var item in items)
                LoadInstanceField(item, fieldName, value, cascade, raisePropertyChanged, updateState);
        }
        partial void OnAfterLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnBeforeLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "cascade"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")]
        public void LoadInstanceField(global::Sidvall.WorkItemManager.Data.IIdentityProvider item, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (item == null)
                return;
            if (fieldName == null)
                return;
            OnBeforeLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
            switch (fieldName)
            {
                case "IdentityProviderId":
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.IdentityProviderId = (System.Nullable<int>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "IdentityProviderId", raisePropertyChanged, updateState);
                    break;
                case "Password":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.Password = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Password", raisePropertyChanged, updateState);
                    break;
                case "STSAddress":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.STSAddress = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "STSAddress", raisePropertyChanged, updateState);
                    break;
                case "STSClientId":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.STSClientId = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "STSClientId", raisePropertyChanged, updateState);
                    break;
                case "STSClientSecret":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.STSClientSecret = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "STSClientSecret", raisePropertyChanged, updateState);
                    break;
                case "UserName":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.UserName = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "UserName", raisePropertyChanged, updateState);
                    break;
                default:
                    OnLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
                    break;
            }
            OnAfterLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region Clone

        public TEntity Clone(IIdentityProvider value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }
        public TList Clone(System.Collections.Generic.IEnumerable<IIdentityProvider> value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }

        #endregion
        #region Copy

        public TEntity Copy(IIdentityProvider value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TEntity Copy(IIdentityProvider value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TEntity Copy(IIdentityProvider value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(value, copySettings.RowFilter))
                return default(TEntity);
            var item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.IdentityProvider, null);
            if (item == null)
                return default(TEntity);
            Import(item, value, copySettings, parent, child);
            return item;
        }

        public TList Copy(System.Collections.Generic.IEnumerable<IIdentityProvider> value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TList Copy(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TList Copy(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            var items = (TList)this.EntityFacadeManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.IdentityProvider);
            Import(items, value, copySettings, parent, child);
            return items;
        }

        #endregion
        #region GetKey

        public string GetKey(IIdentityProvider item)
        {
            if (item == null)
                return null;
            if (item.IdentityProviderId == null)
                return null;
            return item.IdentityProviderId.ToString();
        }
        public string GetKey(int identityProviderId)
        {
            return identityProviderId.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
        public string GetKey(Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.IdentityProviderField> item)
        {
            if (item == null)
                return null;
            var part = item.FilterItems.GetFilterItemValue(Sidvall.WorkItemManager.Data.Fields.IdentityProviderField.IdentityProviderId, null);
            if (part == null)
                return null;
            var value = part;
            return value;
        }

        #endregion
        #region ParseKey

        public System.Nullable<int> ParseKey(string key)
        {
            return key.ToNullableInt32();
        }

        #endregion
        #region Import

        public void Import(TEntity destination, IIdentityProvider source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(TEntity destination, IIdentityProvider source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        partial void OnAfterImport(TEntity destination, IIdentityProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnBeforeImport(TEntity destination, IIdentityProvider source, Sidvall.Data.CopySettings copySettings, ref string parent, ref string child);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "parent"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "child"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        internal void Import(TEntity destination, IIdentityProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (destination == null)
                return;
            if (source == null)
                return;
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            OnBeforeImport(destination, source, copySettings, ref parent, ref child);
            var accessLevelProvider = source as Sidvall.Security.IPropertyAccessLevelProvider;
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int32, "IdentityProvider.IdentityProviderId", "IdentityProvider"))
                    destination.IdentityProviderId = source.IdentityProviderId;
            }
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.Password", "IdentityProvider"))
                    destination.Password = source.Password;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSAddress", "IdentityProvider"))
                    destination.STSAddress = source.STSAddress;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientId", "IdentityProvider"))
                    destination.STSClientId = source.STSClientId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientSecret", "IdentityProvider"))
                    destination.STSClientSecret = source.STSClientSecret;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.UserName", "IdentityProvider"))
                    destination.UserName = source.UserName;
            }
            Sidvall.Data.EntityManager.AfterImportItem(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        public void Import(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, long? totalRowCount, Sidvall.Data.CopySettings copySettings)
        {
            Sidvall.Data.ITotalRowCount rowCountProvider;

            if (source == null)
                return;
            Import(destination, source, copySettings, string.Empty, string.Empty);
            if (totalRowCount != null)
            {
                rowCountProvider = destination as Sidvall.Data.ITotalRowCount;
                if (rowCountProvider != null)
                    rowCountProvider.TotalRowCount = totalRowCount.Value;
            }
        }
        internal void Import(Sidvall.Data.IDataRowProvider<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import(destination.DataItems, source, copySettings, parent, child);
        }
        partial void OnBeforeImport(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnAfterImport(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        internal void Import(System.Collections.Generic.IEnumerable<IIdentityProvider> destination, System.Collections.Generic.IEnumerable<IIdentityProvider> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            OnBeforeImport(destination, source, copySettings, parent, child);
            ImportItems(destination, source, copySettings, parent, child);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        #endregion
        #region DataRowIsValid

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowIsValid(IIdentityProvider value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            if (filter == null)
                filter = Sidvall.Data.BusinessRuleFilter.CreateFilter();
            var provider = value as Sidvall.Data.IValidParentProvider;
            if ((provider != null) && (!provider.IsSelfValid(filter)))
                return false;
            return true;
        }
        public bool DataRowIsValid(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            foreach (var item in value)
            {
                if (!DataRowIsValid(item, filter))
                    return false;
            }
            return true;
        }

        #endregion
        #region DataRowStateIsDirty

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowStateIsDirty(IIdentityProvider value)
        {
            if (value == null)
                return false;
            if (value.DataRowStateIsSelfDirty)
                return true;
            return false;
        }
        public bool DataRowStateIsDirty(System.Collections.Generic.IEnumerable<IIdentityProvider> value)
        {
            return IsDirtyItems(value);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IIdentityProvider value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IIdentityProvider value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        partial void OnBeforeValidate(IIdentityProvider value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        partial void OnAfterValidate(IIdentityProvider value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        public async System.Threading.Tasks.Task ValidateAsync(IIdentityProvider value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnBeforeValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            OnAfterValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.Merge(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IIdentityProvider> value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        public async System.Threading.Tasks.Task ValidateAsync(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            foreach (var item in value)
                await ValidateAsync(item, context).ConfigureAwait(false);
        }

        #endregion
        #region Initialize

        public void Initialize(IIdentityProvider value)
        {
            Initialize(value, null);
        }
        partial void OnAfterInitialize(IIdentityProvider value);
        partial void OnBeforeInitialize(IIdentityProvider value);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void Initialize(IIdentityProvider value, Sidvall.Data.InitializeSettings settings)
        {
            if (value == null)
                return;
            if (settings == null)
                settings = new Sidvall.Data.InitializeSettings();
            OnBeforeInitialize(value);
            var accessLevelProvider = value as Sidvall.Security.IPropertyAccessLevelProvider;
            switch (settings.FieldInitializeOption)
            {
                case Sidvall.Data.FieldInitializeOption.Reset:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int32, "IdentityProvider.IdentityProviderId", "IdentityProvider"))
                            value.IdentityProviderId = null;
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.Password", "IdentityProvider"))
                            value.Password = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSAddress", "IdentityProvider"))
                            value.STSAddress = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientId", "IdentityProvider"))
                            value.STSClientId = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientSecret", "IdentityProvider"))
                            value.STSClientSecret = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.UserName", "IdentityProvider"))
                            value.UserName = string.Empty;
                    }
                    break;
                case Sidvall.Data.FieldInitializeOption.Validate:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int32, "IdentityProvider.IdentityProviderId", "IdentityProvider"))
                            value.IdentityProviderId = Sidvall.Business.ValidationManager.GetValidInt32(value.IdentityProviderId, null, null);
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.Password", "IdentityProvider"))
                            value.Password = Sidvall.Business.ValidationManager.GetValidString(value.Password, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSAddress", "IdentityProvider"))
                            value.STSAddress = Sidvall.Business.ValidationManager.GetValidString(value.STSAddress, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientId", "IdentityProvider"))
                            value.STSClientId = Sidvall.Business.ValidationManager.GetValidString(value.STSClientId, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.STSClientSecret", "IdentityProvider"))
                            value.STSClientSecret = Sidvall.Business.ValidationManager.GetValidString(value.STSClientSecret, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "IdentityProvider.UserName", "IdentityProvider"))
                            value.UserName = Sidvall.Business.ValidationManager.GetValidString(value.UserName, false, null, true);
                    }
                    break;
                default:
                    break;
            }
            if (settings.ClearBusinessRules)
            {
                var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
                if (businessRulesProvider != null)
                    businessRulesProvider.BusinessRules.Clear();
            }
            OnAfterInitialize(value);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IIdentityProvider> value)
        {
            Initialize(value, null);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IIdentityProvider> value, Sidvall.Data.InitializeSettings settings)
        {
            InitializeItems(value, settings);
        }

        #endregion
        #region EntityManagerBase Members

        protected override Sidvall.Data.IDataRowContextProvider CopyItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            return Copy(destination as IIdentityProvider, copySettings, parent, child);
        }
        protected override void ImportItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.IDataRowContextProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import((TEntity)destination, source as IIdentityProvider, copySettings, parent, child);
        }
        protected override void AcceptItemChanges(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptChanges(item as IIdentityProvider, acceptChangesSettings);
        }
        protected override bool IsDirtyItem(Sidvall.Data.IDataRowContextProvider item)
        {
            return DataRowStateIsDirty(item as IIdentityProvider);
        }
        protected override void InitializeItem(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.InitializeSettings settings)
        {
            Initialize(item as IIdentityProvider, settings);
        }

        #endregion

        #endregion
        #region Private Members

        private global::Sidvall.WorkItemManager.Data.ConfigModel.IEntityFacadeManager EntityFacadeManager { get; set; }

        #endregion
        #region Constructors

        public IdentityProviderManager(global::Sidvall.WorkItemManager.Data.ConfigModel.IEntityFacadeManager entityFacadeManager)
        {
            this.EntityFacadeManager = entityFacadeManager;
        }

        #endregion
    }
}